package com.ruoyi.web.controller.tool

import java.util.ArrayList
import java.util.LinkedHashMap
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import com.ruoyi.common.core.controller.BaseController
import com.ruoyi.common.core.domain.R
import com.ruoyi.common.utils.MyStringUtils
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import io.swagger.annotations.ApiOperation

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
open class TestController : BaseController() {
    init {
        users[1] = UserEntity(1, "admin", "admin123", "15888888888")
        users[2] = UserEntity(2, "ry", "admin123", "15666666666")
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    open  fun userList(): R<List<UserEntity>> {
        val userList: ArrayList<UserEntity> = ArrayList<UserEntity>(users.values)
        return R.ok(userList)
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer::class)
    @GetMapping("/{userId}")
    open  fun getUser(@PathVariable userId: Int?): R<UserEntity?> {
        return if (users.isNotEmpty() && users.containsKey(userId)) {
            R.ok(users[userId])
        } else {
            R.fail("用户不存在")
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParams(
        ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer::class),
        ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String::class),
        ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String::class),
        ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String::class)
    )
    @PostMapping("/save")
    open  fun save(user: UserEntity): R<String?> {
        if (MyStringUtils.isNull(user) || MyStringUtils.isNull(user.userId)) {
            return R.fail("用户ID不能为空")
        }
        users[user.userId] = user
        return R.ok()
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    open  fun update(@RequestBody user: UserEntity): R<String?> {
        if (MyStringUtils.isNull(user) || MyStringUtils.isNull(user.userId)) {
            return R.fail("用户ID不能为空")
        }
        if (users.isEmpty() || !users.containsKey(user.userId)) {
            return R.fail("用户不存在")
        }
        users.remove(user.userId)
        users[user.userId] = user
        return R.ok()
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer::class)
    @DeleteMapping("/{userId}")
    open  fun delete(@PathVariable userId: Int?): R<String?> {
        return if (users.isNotEmpty() && users.containsKey(userId)) {
            users.remove(userId)
            R.ok()
        } else {
            R.fail("用户不存在")
        }
    }

    companion object {
        private val users: MutableMap<Int?, UserEntity> = LinkedHashMap<Int?, UserEntity>()
    }
}

@ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity {
    @ApiModelProperty("用户ID")
    var userId: Int? = null

    @ApiModelProperty("用户名称")
    var username: String? = null

    @ApiModelProperty("用户密码")
    var password: String? = null

    @ApiModelProperty("用户手机")
    var mobile: String? = null

    constructor()
    constructor(userId: Int?, username: String?, password: String?, mobile: String?) {
        this.userId = userId
        this.username = username
        this.password = password
        this.mobile = mobile
    }

}
